package com.zy.scworder.service.impl;

import java.util.UUID;

import com.zy.scwcommon.bean.AppResponse;
import com.zy.scwcommon.enums.OrderStatusEnumes;
import com.zy.scwcommon.util.AppDateUtils;
import com.zy.scworder.bean.TOrder;
import com.zy.scworder.mapper.TOrderMapper;
import com.zy.scworder.service.TOrderService;
import com.zy.scworder.service.TProjectServiceFeign;
import com.zy.scworder.vo.req.OrderInfoSubmitVo;
import com.zy.scworder.vo.resp.TReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class TOrderServiceImpl implements TOrderService {

	@Autowired
	StringRedisTemplate stringRedisTemplate;
	
	@Autowired
	TOrderMapper orderMapper ;
	
	@Autowired
	TProjectServiceFeign projectServiceFeign;
	
	@Override
	public TOrder saveOrder(OrderInfoSubmitVo vo) {
		TOrder order = new TOrder();
		log.debug("业务层保存订单---开始={}",vo);
		String accessToken = vo.getAccessToken();
		String memberId = stringRedisTemplate.opsForValue().get(accessToken);
		
		order.setMemberid(Integer.parseInt(memberId));
		
		order.setProjectid(vo.getProjectid());
		order.setReturnid(vo.getReturnid());
		//订单号id
		String ordernum = UUID.randomUUID().toString().replaceAll("-", "");
		order.setOrdernum(ordernum);
		
		order.setCreatedate(AppDateUtils.getFormatTime());
		//调用远程服务order--->project
		AppResponse<TReturn> respTReturn = projectServiceFeign.returnInfo(vo.getReturnid()) ;

		TReturn retObj = respTReturn.getData();
		
		Integer totalMoney = vo.getRtncount() * retObj.getSupportmoney() + retObj.getFreight() ;
		
		order.setMoney(totalMoney);
		order.setRtncount(vo.getRtncount());
		order.setStatus(OrderStatusEnumes.UNPAY.getCode()+"");
		order.setAddress(vo.getAddress());
		order.setInvoice(vo.getInvoice().toString());
		order.setInvoictitle(vo.getInvoictitle());
		order.setRemark(vo.getRemark());
		
		orderMapper.insertSelective(order);
		
		log.debug("业务层保存订单---结束={}",order);
		
		return order;
	}

}
